
package com.compuvest.admin.update.v1

class Item implements Serializable {

    final static int MAX_WORD_WIDTH = 25

    def category = [:], vendor = [:], field = [:], fdesc = [:], pref = [:], advert = [:], banner = [:]
    def utils = [:], json = [:], images = [:], history = [:], needupdate = [:], quantity = [:]
    def result, resulttype, resultmessage

    void setResult(_result, _resulttype, _resultmessage) {
        result = _result
        resulttype = _resulttype
        if(! resultmessage) { //empty
            resultmessage = _resultmessage
        }
        //resultmessage = _resultmessage
    }

    // prepare search keywords
    String getKeywords() {

        def thesaurus = new StringBuilder()
        thesaurus << fdesc['etilid']
        thesaurus << " ${utils['accwareid']}"
        thesaurus << " ${utils['accwareid'].replaceAll(/\-../, '')}"        // 452001880-00 -> 452001880
        thesaurus << " ${field['mfrname']}"
        thesaurus << " ${field['mfrnumber']}"
        thesaurus << " ${field['mfrnumber'].replaceAll(/\#.*/, '')}"        // ABA#777 -> ABA
        thesaurus << " ${advert['advmfrname']}"
        thesaurus << " ${advert['advmfrnumber'].replaceAll(/\#.*/, '')}"    // ABA#777 -> ABA
        thesaurus << " ${field['itemname']}"
        thesaurus << " ${field['advname']}"
        thesaurus << " ${advert['keywords']}"
        thesaurus << " ${json['namea']}"
        thesaurus << " ${json['nameb']}"
        thesaurus << " ${json['pkeywrd']}"

        return swords(thesaurus.toString().toLowerCase()).toString().replaceAll(/\,|\[|\]|\#|\/|\.|\-|\(|\)/, '') // delete , [ ] # / . - ( )
    }

    def swords(s) {

        def result = []
	s.split(/\p{Space}/).each { word ->

            if(word.length() < MAX_WORD_WIDTH) {

		if("hp" == word) word = "hpp"

                if(word.length() > 2) {
                    result += word
                }
            }
        }

	return result as Set // only unique words
    }

    def getMap = { // new item

        [
            sku     : utils.accwareid,
            etilid  : fdesc.etilid,
            stock   : (field.stock == 'Y') ? true : false,
            keywords: getKeywords(),
            weight  : field.weight,
            drone   : field.drone,
            sockdate: field.stockdate,
            json    : getJSON(),
            drop    : false,
            quantity: [
                stock : quantity.qty,
                reserv: 0,
                sell  : 0
            ],
            names: [
                name: field.itemname,
                a   : json.namea,
                b   : json.nameb
            ],
            mfr: [
                name  : field.mfrname,
                number: field.mfrnumber,
                link  : field.mfrlink
            ],
            price: [
                cost  : field.costprice,
                sell  : field.sellprice,
                hot   : field.reslprice,
                drop  : 0,
                backup: 0
            ],
            reseller: [
                price1: field.reslprice,
                price2: field.reslprice2,
                price3: field.reslprice3,
                qty2  : field.reslqty2,
                qty3  : field.reslqty3
            ],
            warranty: [
                mask  : field.wtymask,
                price1: field.wtyprice1,
                price2: field.wtyprice2,
                price3: field.wtyprice3
            ],
            vendor: [
                alias: vendor.alias,
                name : vendor.name
            ],
            imgs: json.pics,
            packag: [
                include: field.packinclude,
                comment: field.packcomment
            ],
            google: [
                group  : field.googlegroup,
                action : '',
                update : '',
                expdate: '',
                answer : '',
                send   : false
            ],
            preference: [
                p1: pref.pref1,
                p2: pref.pref2,
                p3: pref.pref3,
                p4: pref.pref4,
                p5: pref.pref5
            ],
            advert: [
                name       : field.advname,
                price      : (field.advprice == 'Y') ? true : false,
                mfrname    : advert.advmfrname,
                mfrnumber  : advert.advmfrnumber,
                keywords   : advert.keywords,
                cnet       : advert.cnet,
                grabber    : advert.grabber,
                bizrate    : advert.bizrate,
                nextag     : advert.nextag,
                shopping   : advert.shopping,
                powersource: advert.powersource,
                froogle    : advert.froogle,
                cj         : advert.cj,
                gadgetpage : advert.gadgetpage,
                upccode    : advert.upccode
            ],
            banner: [
                special     : banner.special,
                closeout    : banner.closeout,
                comingsoon  : banner.comingsoon,
                bestprice   : banner.bestprice,
                firstpage   : banner.firstpage,
                categorypage: banner.categorypage,
                ebay        : banner.ebay,
                lot         : banner.lot
            ],
            promo: [[
                code   : '',
                percent: 0,
                price  : 0,
                used   : 0,
                enddate: ''
            ]],
            update : new Date(),
            create : new Date(),
            upcount: 1,
            active : 'Y'
        ]
    }

/*
                                    category: [
                                        parentid:       item.category.parentid.text(),
                                        parentname:     item.category.parentname.text(),
                                        categoryid:     item.category.categoryid.text(),
                                        catname:        item.category.catname.text(),
                                        prefname1:      clean(item.category.prefname1.text()),
                                        prefname2:      clean(item.category.prefname2.text()),
                                        prefname3:      clean(item.category.prefname3.text()),
                                        prefname4:      clean(item.category.prefname4.text()),
                                        prefname5:      clean(item.category.prefname5.text())
                                    ],
                                    fdesc: [
                                        desc:           udesc,
                                        size:           udesc.length(),
                                        etilid:         0L
                                    ],
*/

    String getJSON() {

        def js = new StringBuilder()
        js << "{"
        js << "type:\"E\","
        js << "eid:\"${fdesc['etilid']}\","
        js << "sku:\"${utils['accwareid']}\","
        js << "name:\"${field['itemname']}\","
        js << "advname:\"${field['advname']}\","
        js << "namea:\"${json['namea']}\","
        js << "nameb:\"${json['nameb']}\","
        js << "mfrname:\"${field['mfrname']}\","
        js << "mfrnumber:\"${field['mfrnumber']}\","
        js << "qty:${quantity['qty']},"
        js << "stock:\"${field['stock']}\","
        js << "sprc:${field['sellprice']},"
        js << "hprc:${field['reslprice']},"
        js << "r1prc:${field['reslprice1']},"
        js << "r2prc:${field['reslprice2']},"
        js << "r3prc:${field['reslprice3']},"
        js << "r2qty:${field['reslqty2']},"
        js << "r3qty:${field['reslqty3']},"
        js << "wrmsk:${field['wtymask']},"
        js << "wr1prc:${field['wtyprice1']},"
        js << "wr2prc:${field['wtyprice2']},"
        js << "wr3prc:${field['wtyprice3']},"
        js << "mfrpn:\"${field['mfrnumber']}\","
        js << "minfo:\"${json['minfo']}\","
        js << "mspec:{${json['mspec']}},"
        js << "espec:[${json['espec']}],"
        js << "psiml:\"${json['psiml']}\","
        js << "paccl:\"${json['paccl']}\","
        js << "sattr:{${json['sattr']}},"
        js << "imgs:[${json['imgs']}],"
        js << "pdf:[${json['pdf']}]"
        js << "}"
    }

    String toString() {

        def out = new StringBuilder();

        out << "\nutils:\t${utils}"

        return out
    }

}

